package com.aurora.oa.work.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.adapter.CommonRecyclerViewAdapter;
import com.aurora.oa.calendar.cons.DPMode;
import com.aurora.oa.calendar.views.MonthView;
import com.aurora.oa.calendar.views.WeekView;
import com.aurora.oa.listener.OnRequestPatrollogListListener;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.work.bean.PatrollogListBean;
import com.aurora.oa.work.bean.WorkLogDataBean;
import com.codbking.calendar.CalendarUtil;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/2 0002.
 */
public class PatrolShopDetailActivity extends BaseActivity implements MonthView.OnDateChangeListener, MonthView.OnDatePickedListener {
    @Bind(R.id.tv_data)
    TextView mTitle;
    @Bind(R.id.activity_main)
    LinearLayout activityMain;
    @Bind(R.id.iv_back_month)
    ImageView ivBackMonth;
    @Bind(R.id.iv_foword_month)
    ImageView ivFowordMonth;
    CommonRecyclerViewAdapter<WorkLogDataBean> adapter;
    String currentDate;
    List<WorkLogDataBean> logDataBeanList = new ArrayList<>();
    TextView tvRemark;

    private MonthView monthView;
    private WeekView weekView;
    private LinearLayout contentLayout;
    private TextView weekTxt;
    private Calendar now;

    //  private WorkLogMySendActivityPresenter presenter = new WorkLogMySendActivityPresenterImpl(this);
    private RecyclerView.LayoutManager linearLayoutManager;
    private String shopid;
    private RequestHttpModel model = new RequestHttpModelImpl();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitlebar(true, "店铺日志", true);
        setMyContentView(R.layout.activity_shop_log_list);
        ButterKnife.bind(this);
        initView();
        //获取今天的日志数据
        initData();
        // initList();
    }


    private void initData() {
        model.getPatrollogListByShopAndDay(shopid, currentDate, new OnRequestPatrollogListListener() {
            @Override
            public void onRequestPatrollogListSuccess(List<PatrollogListBean> patrollogListBeanList) {
                initList(patrollogListBeanList);
            }

            @Override
            public void onRequestPatrollogListFail(String message) {

            }
        });
    }


    private void initView() {
        shopid = getIntent().getStringExtra("id");
        now = Calendar.getInstance();
        monthView = (MonthView) findViewById(R.id.month_calendar);
        weekView = (WeekView) findViewById(R.id.week_calendar);
        contentLayout = (LinearLayout) findViewById(R.id.content_layout);
        weekTxt = (TextView) findViewById(R.id.week_text);

        monthView.setDPMode(DPMode.SINGLE);
        monthView.setDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH) + 1);
        monthView.setFestivalDisplay(true);
        monthView.setTodayDisplay(true);
        monthView.setOnDateChangeListener(this);
        monthView.setOnDatePickedListener(this);

        weekView.setDPMode(DPMode.SINGLE);
        weekView.setDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH) + 1);
        weekView.setFestivalDisplay(true);
        weekView.setTodayDisplay(true);
        weekView.setOnDatePickedListener(this);

        int[] data = CalendarUtil.getYMD(new Date());
        mTitle.setText(data[0] + "年" + data[1] + "月");
        //月份日期补0，服务器需要
        String month = data[1] + "";
        if (month.length() == 1) {
            month = "0" + month;
        }
        String day = data[2] + "";
        if (day.length() == 1) {
            day = "0" + day;
        }

        currentDate = data[0] + "-" + month + "-" + day;
    }

    @OnClick({R.id.iv_back_month, R.id.iv_foword_month})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back_month:
                monthView.moveBack();
                break;
            case R.id.iv_foword_month:
                monthView.moveForwad();
                break;
        }
    }

    public void initList(List<PatrollogListBean> logDataList) {
        contentLayout.removeAllViews();
        for (int i = 0; i < logDataList.size(); i++) {
            View itemView = View.inflate(this, R.layout.item_detail_patrol_log, null);
            ViewHolder viewHolder = new ViewHolder(itemView);
            viewHolder.tvPatrolPerson.setText(logDataList.get(i).realname);
            viewHolder.tvPatrolTime.setText(logDataList.get(i).createtime);
            viewHolder.tvRemark.setText(logDataList.get(i).remark);
            viewHolder.tvSellMoney.setText(logDataList.get(i).sales);
            viewHolder.tvShopName.setText(logDataList.get(i).shopname);
            contentLayout.addView(itemView);
        }
    }

    @Override
    public void onDateChange(int year, int month) {
        mTitle.setText(year + "年" + month + "月");
    }

    @Override
    public void onDatePicked(String date) {
        String[] dataArray = date.split("\\.");

        //月份日期补0，服务器需要
        String month = dataArray[1] + "";
        if (month.length() == 1) {
            month = "0" + month;
        }
        String day = dataArray[2] + "";
        if (day.length() == 1) {
            day = "0" + day;
        }
        currentDate = dataArray[0] + "-" + month + "-" + day;
        initData();
    }



    class ViewHolder {
        @Bind(R.id.tv_shop_name)
        TextView tvShopName;
        @Bind(R.id.tv_patrol_person)
        TextView tvPatrolPerson;
        @Bind(R.id.tv_patrol_time)
        TextView tvPatrolTime;
        @Bind(R.id.tv_sell_money)
        TextView tvSellMoney;
        @Bind(R.id.tv_remark)
        TextView tvRemark;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
